<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
          v-hasPermi="['product:productImage:edit']"
          >修改</el-button
        >
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList">
      <el-tab le-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <el-table-column
        label="字体大小"
        :show-overflow-tooltip="true"
        align="center"
        prop="productName"
      />
      <el-table-column
        label="字体颜色"
        :show-overflow-tooltip="true"
        align="center"
        prop="productMaterial"
      >
        <template slot-scope="scope">
          <el-color-picker
            disabled="true"
            v-model="scope.row.productMaterial"
          ></el-color-picker>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:product:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="1">
          <el-col :span="10">
            <el-form-item label="字体大小" prop="productName">
              <el-input-number
                v-model="form.productName"
                controls-position="right"
                @change="handleChange"
                :min="1"
                :max="50"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="1">
          <el-col :span="10">
            <el-form-item label="字体颜色" prop="productMaterial">
              <el-color-picker
                show-alpha="true"
                v-model="form.productMaterial"
              ></el-color-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProduct,
  getProduct,
  delProduct,
  addProduct,
  updateProduct,
} from "@/api/product/product";
import { delProductImage } from "@/api/product/productImage";
import { listData } from "@/api/system/dict/data";
import { listProductClass } from "@/api/product/productClass";
import { getToken } from "@/utils/auth";
export default {
  name: "NetWorkMenu",
  data() {
    return {
      menuObj: null,
      fileList: [],
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      value: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productPrice: null,
        classIds: null,
        undercarriage: null,
        productMaterial: null,
        productHigh: null,
        productWidth: null,
        productLength: null,
        productModel: null,
        productBrand: null,
        productImage: null,
        typeId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        version: null,
        productColor: null,
      },
      // 查询产品类型参数
      queryTypeParams: {
        pageNum: 1,
        pageSize: 10,
        dictName: undefined,
        dictType: "product_type",
        status: undefined,
      },
      //产品类型
      typeList: [],
      imageList: [],
      //产品分类
      classifiedList: [],
      // 表单参数
      form: {
        id: 32,
        instructions: null,
        detailed: null,
        productName: null,
        productPrice: null,
        classId: null,
        classIds: null,
        undercarriage: null,
        productMaterial: null,
        productHigh: null,
        productWidth: null,
        productLength: null,
        productModel: null,
        productBrand: null,
        productImage: null,
        typeId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        version: null,
        productColor: null,
        imageList: [],
      },
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        // 上传的文件列表
        fileList: [],
      },
      // 表单校验
      rules: {
        productName: [{ required: true, message: "产品名称不能为空", trigger: "blur" }],
        productPrice: [{ required: true, message: "产品价格不能为空", trigger: "blur" }],
        undercarriage: [{ required: true, message: "是否下架不能为空", trigger: "blur" }],
        productHigh: [{ required: true, message: "产品高度不能为空", trigger: "blur" }],
        productWidth: [{ required: true, message: "产品宽度不能为空", trigger: "blur" }],
        productLength: [{ required: true, message: "产品长度不能为空", trigger: "blur" }],
        productBrand: [{ required: true, message: "产品品牌不能为空", trigger: "blur" }],
        productImage: [{ required: true, message: "产品图片不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleRemove(file, fileList) {
      delProductImage(file.name).then((res) => {
        console.log(res);
      });
    },
    handlePreview(file, fileList) {
      console.log(file);
    },
    handleDownload(file) {
      console.log(file);
    },

    handleChange(value) {
      console.log(value);
    },
    /** 查询产品列表 */
    getList() {
      this.loading = true;
      getProduct(32).then((response) => {
        this.menuObj = response.data;
        this.productList = [];
        this.productList.push(this.menuObj);
        this.loading = false;
      });
    },
    /**
     * 查询产品分类
     */
    getProductClassList() {
      listProductClass(this.addDateRange(this.queryTypeParams)).then((response) => {
        this.classifiedList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询产品类型 */
    getProductTypeList() {
      listData(this.addDateRange(this.queryTypeParams)).then((response) => {
        // console.log(response);
        this.typeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: 32,
        productName: null,
        detailed: null,
        instructions: null,
        productPrice: null,
        classId: null,
        classIds: null,
        undercarriage: null,
        productMaterial: null,
        productHigh: null,
        productWidth: null,
        productLength: null,
        productModel: null,
        productBrand: null,
        productImage: null,
        typeId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        version: null,
        productColor: null,
        imageList: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.classIds = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品";
      this.upload.fileList = [];
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.upload.fileList = [];
      const id = 32;
      getProduct(id).then((response) => {
        this.form = response.data;
        console.log(this.form.imageList);
        for (let i = 0; i < this.form.imageList.length; i++) {
          let file = {
            name: this.form.imageList[i].id,
            url: this.form.imageList[i].imageAddress,
          };

          this.upload.fileList.push(file);
        }
        console.log(this.upload.fileList);
        this.open = true;
        this.title = "菜单设置";
        // this.upload.fileList = [];
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.imageList = this.imageList;
            updateProduct(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.imageList = this.imageList;
            addProduct(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除产品编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProduct(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "product/product/export",
        {
          ...this.queryParams,
        },
        `product_${new Date().getTime()}.xlsx`
      );
    },
    // 文件提交处理
    submitUpload() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      console.log(response);
      this.form.filePath = response.url;
      let image = {
        imageAddress: this.form.filePath,
        productId: this.form.id,
      };
      this.imageList.push(image);
      this.$message({
        message: "上传成功",
        type: "success",
      });
      // this.msgSuccess(response.msg);
    },
  },
};
</script>
